# $Id: search.tcl 44 2007-09-03 00:47:25Z khomoutov $
# Test scripts that (re)creates files database with one share
# rooted at specified directory and populates that share.

lappend auto_path tcldc
package require dcshare
namespace import ::dcshare::*

if {$argc < 1} {
	puts stderr "Usage: $argv0 ?-type mediatype? -keywords keyword \[keyword ...\]?"
	exit 1
}

# Description:
#  Pop the nth element off a list. Used in options processing.
#
proc Pop {varname {nth 0}} {
    upvar $varname args
    set r [lindex $args $nth]
    set args [lreplace $args $nth $nth]
    return $r
}

set cmd [list database search]

while {[string match -* [lindex $argv 0]]} {
	switch -glob -- [lindex $argv 0] {
		-key* {
			lappend cmd -keywords [lrange $argv 1 end]
			break
		}
		default {
			lappend cmd [lindex $argv 0] [Pop argv 1]
		}
	}
	Pop argv
}

database open ./scratch.db

puts $cmd
puts [time {
	set results [eval $cmd]
} 1]
puts [join $results \n]

database close

